﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skywave.IO.Compression
{
    public class ZipStorerProvider_File : IZipStorerProvider
    {
        public Task<Stream> File_CreateStream(string filePath)
        {
            TaskFactory<Stream> r_TaskFactory = new TaskFactory<Stream>();
            return r_TaskFactory.StartNew(() =>
            {
                return new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
            });
        }

        public Task<Stream> File_OpenStream(string filePath, bool needWriteAccess)
        {
            TaskFactory<Stream> r_TaskFactory = new TaskFactory<Stream>();
            return r_TaskFactory.StartNew(() =>
            {
                return new FileStream(filePath, FileMode.Open, (needWriteAccess) ? FileAccess.ReadWrite : FileAccess.Read);
            });
        }

        public Task<DateTime> File_GetLastWriteTime(string filePath)
        {
            TaskFactory<DateTime> r_TaskFactory = new TaskFactory<DateTime>();
            return r_TaskFactory.StartNew(() =>
            {
                return File.GetLastWriteTime(filePath);
            });
        }

        public Task File_CloseStream(Stream stream)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                if (stream is FileStream)
                    (stream as FileStream).Close();
            });
        }

        public Task<string> Directory_GetFullName(string dirPath)
        {
            TaskFactory<string> r_TaskFactory = new TaskFactory<string>();
            return r_TaskFactory.StartNew(() =>
            {
                return new DirectoryInfo(dirPath).FullName;
            });
        }

        public Task<bool> Directory_Exists(string dirPath)
        {
            TaskFactory<bool> r_TaskFactory = new TaskFactory<bool>();
            return r_TaskFactory.StartNew(() =>
            {
                return Directory.Exists(dirPath);
            });
        }

        public Task Directory_Create(string dirPath)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                Directory.CreateDirectory(dirPath);
            });
        }

        public Task File_SetCreationTime(string filePath, DateTime dateTime)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                File.SetCreationTime(filePath, dateTime);
            });
        }

        public Task File_SetLastWriteTime(string filePath, DateTime dateTime)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                File.SetLastWriteTime(filePath, dateTime);
            });
        }

        public Task<bool> Stream_IsFile(Stream stream)
        {
            TaskFactory<bool> r_TaskFactory = new TaskFactory<bool>();
            return r_TaskFactory.StartNew(() =>
            {
                return (stream is FileStream);
            });
        }

        public Task File_Delete(string filePath)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                File.Delete(filePath);
            });
        }

        public Task File_Move(string pathSource, string pathDest)
        {
            TaskFactory r_TaskFactory = new TaskFactory();
            return r_TaskFactory.StartNew(() =>
            {
                File.Move(pathSource, pathDest);
            });
        }

        public Task<string> Path_GetTempFileName()
        {
            TaskFactory<string> r_TaskFactory = new TaskFactory<string>();
            return r_TaskFactory.StartNew(() =>
            {
                return Path.GetTempFileName();
            });
        }

        public Task<bool> File_Exists(string filePath)
        {
            TaskFactory<bool> r_TaskFactory = new TaskFactory<bool>();
            return r_TaskFactory.StartNew(() =>
            {
                return File.Exists(filePath);
            });
        }

        public Task<string> Directory_GetName(string dirPath)
        {
            TaskFactory<string> r_TaskFactory = new TaskFactory<string>();
            return r_TaskFactory.StartNew(() =>
            {
                return new DirectoryInfo(dirPath).Name;
            });
        }

        public Task<IEnumerable<string>> Directory_GetFiles(string dirPath)
        {
            TaskFactory<IEnumerable<string>> r_TaskFactory = new TaskFactory<IEnumerable<string>>();
            return r_TaskFactory.StartNew(() =>
            {
                return Directory.GetFiles(dirPath);
            });
        }

        public Task<string> File_GetFullName(string filePath)
        {
            TaskFactory<string> r_TaskFactory = new TaskFactory<string>();
            return r_TaskFactory.StartNew(() =>
            {
                return new FileInfo(filePath).FullName;
            });
        }

        public Task<string> File_GetName(string filePath)
        {
            TaskFactory<string> r_TaskFactory = new TaskFactory<string>();
            return r_TaskFactory.StartNew(() =>
            {
                return new FileInfo(filePath).Name;
            });
        }

        public Task<IEnumerable<string>> Directory_GetDirectories(string dirPath)
        {
            TaskFactory<IEnumerable<string>> r_TaskFactory = new TaskFactory<IEnumerable<string>>();
            return r_TaskFactory.StartNew(() =>
            {
                return Directory.GetDirectories(dirPath);
            });
        }
    }
}
